package com.cloudptt.api.comm;


import com.cloudptt.api.product.res.Resource;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.Toast;


public class CommDlg
{
	private static Toast mToast = null;
	
	public static void showShortToast(Context context, String text)
	{
		show(context, text);
		
	}
	
	public static void showToast(Context context, int resId){
		String str;
		if(context == null){
			return;
		}else{
			str = context.getResources().getString(resId);
		}
		showToast(context, str);
	}
	
	public static void showToast(Context context, String text){
		show(context, text);
	}
	
	private static void showToast(Context context, String text, int showType){
		
		if(text == null
				|| text.length() == 0)
		{
			return;
		}
		
		showToast(context, text);
		
	}
	
	public static void show(Context context, CharSequence text){
		show(context, text, Toast.LENGTH_LONG);
	}
	
	public static void show(Context context, CharSequence text, int toastDuration){
		if(context == null)
		{
			return;
		}
		if(text == null
				|| text.length() == 0)
		{
			return;
		}
		
		try {
			mToast.getView().isShown();
			mToast.setText(text);
		} catch (Exception e) {
			// TODO: handle exception
			mToast = Toast.makeText(context, text, toastDuration);
			
			/*View toastRoot = LayoutInflater.from(context).inflate(MResource.getIdByName(context, "layout", "toast_view"), null, false);
			mToast = new Toast(context);
			mToast.setView(toastRoot);
			TextView info = (TextView) toastRoot.findViewById(MResource.getIdByName(context, "id", "TextViewInfo"));
			info.setText(text);*/
			
			mToast.setGravity(Gravity.CENTER, 0, 0);
			
		}
		mToast.show();
	}
		
	public static void clearToast()
	{
		if(mToast != null) {    
			mToast = null;    
		}
	}

	public static void showConfirmDlg(Context context, String causeStr, DialogInterface.OnClickListener okListener, DialogInterface.OnClickListener cancelListener)
	{
		if(causeStr == null
				|| causeStr.length() == 0)
		{
			return;
		}
		
		String confirm = Resource.getString("confirm");
		String cancel = Resource.getString("cancel");
		new AlertDialog.Builder(context)
		.setTitle(confirm)
		.setMessage(causeStr)
		.setNegativeButton(confirm, okListener)
		.setPositiveButton(cancel, cancelListener)
		.setCancelable(false).show();
	}

	public static void showConfirmDlg(Context context, String causeStr, DialogInterface.OnClickListener okListener)
	{
		if(causeStr == null
				|| causeStr.length() == 0)
		{
			return;
		}

		String confirm = Resource.getString("confirm");
		String cancel = Resource.getString("cancel");
		new AlertDialog.Builder(context)
				.setTitle(confirm)
				.setMessage(causeStr)
				.setNegativeButton(confirm, okListener)
				.setCancelable(false).show();
	}

	public static void showWarrningDlg(Context context,String causeStr,DialogInterface.OnClickListener listener)
	{
		if(causeStr == null
				|| causeStr.length() == 0)
		{
			return;
		}
		
		String alert = Resource.getString("alert");
		String confirm = Resource.getString("confirm");

		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setTitle(alert);
		builder.setMessage(causeStr);
		builder.setNegativeButton(confirm, listener);
		//	    builder.setCancelable(false);
		AlertDialog dialog = builder.create();
		dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
		dialog.setCanceledOnTouchOutside(false);
		dialog.show();
	}

	public static AlertDialog showVoiceReportDlg(Context context,String causeStr)
	{
		String str = Resource.getString("voice_report");

		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setTitle(str);
		builder.setMessage(causeStr);
		//	    builder.setNegativeButton(context.getString(R.string.confirm), null);
		builder.setCancelable(false);
		AlertDialog dialog = builder.create();
		dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
		dialog.setCanceledOnTouchOutside(false);
		dialog.show();

		return dialog;
	}
	
	public static void showNormalDlg(Context context, String title, String message,DialogInterface.OnClickListener okListener,DialogInterface.OnClickListener cancelListener){
		showDialog(context, title, message, null, okListener, null, cancelListener);
	}

	public static void showDialog(Context context, String title, String message,DialogInterface.OnClickListener okListener,DialogInterface.OnClickListener cancelListener){
		showDialog(context, title, message, null, okListener, null, cancelListener);
	}
	
	public static void showDialog(Context context, String title, String message, String okStr, DialogInterface.OnClickListener okListener, String cancelStr, DialogInterface.OnClickListener cancelListener){
		String msg = null;
		String dialogTitle= null;
		if(message != null && message.length() != 0){
			msg = message;
		}
		if(title != null && title.length() != 0){
			dialogTitle = title;
		}
		String confirm;
		if(okStr == null){
			confirm = Resource.getString("confirm");
		}else{
			confirm = okStr;
		}
		
		String cancel;
		if(cancelStr == null){
			cancel = Resource.getString("cancel");
		}else{
			cancel = cancelStr;
		}
		
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		
		if(dialogTitle != null){
			builder.setTitle(dialogTitle);
		}
		
		if(msg != null){
			builder.setMessage(msg);
		}
		
		
		if(okListener != null){
			builder.setPositiveButton(confirm, okListener);
		}
		
		if(cancelListener != null){
			builder.setNegativeButton(cancel, cancelListener);
		}
		
		builder.create().show();
	}
	
	public static void showDialog(Context context, String title, String[] itemList,DialogInterface.OnClickListener itemListener){
//		String msg = null;
		String dialogTitle= null;
		if(title != null && title.length() != 0){
			dialogTitle = title;
		}
//		String confirm = Resource.getString("confirm");
		String cancel = Resource.getString("cancel");
		
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		
		if(dialogTitle != null){
			builder.setTitle(dialogTitle);
		}
		
		if(itemList != null){
			builder.setItems(itemList, itemListener);
		}
		
		
//		if(okListener != null){
//			builder.setPositiveButton(confirm, okListener);
//		}
		
		builder.setNegativeButton(cancel, null);
		
		builder.create().show();
	}
	
	
	public static void showDialog(Context context, String title, View  view,final IOnBtnClick btnClick){
//		String msg = null;
		String dialogTitle= null;
		if(title != null && title.length() != 0){
			dialogTitle = title;
		}
		String confirm = Resource.getString("confirm");
//		String cancel = Resource.getString("cancel");
		
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setView(view);
		if(dialogTitle != null){
			builder.setTitle(dialogTitle);
		}
		builder.setPositiveButton(confirm, null);
//		builder.setNegativeButton(cancel, null);
		
		final AlertDialog  dlg = builder.create();
		dlg.show();
		
		View.OnClickListener okListener = new View.OnClickListener(){
			@Override
			public void onClick(View view) {
				// TODO Auto-generated method stub
				boolean isDataOk = btnClick.isDataOk();
				if(isDataOk)
				{
					dlg.dismiss();
				}
			}
		};	
		
		dlg.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(okListener);
	}
	
	/**
	 * 得到自定义的progressDialog
	 * @param context
	 * @param msg
	 * @return
	 */
	//	public static Dialog createLoadingDialog(Context context, String msg) {  
	//		  
	//        LayoutInflater inflater = LayoutInflater.from(context);  
	//        View v = inflater.inflate(R.layout.loading_dialog, null);// 得到加载view  
	//        LinearLayout layout = (LinearLayout) v.findViewById(R.id.dialog_view);// 加载布局  
	//        // main.xml中的ImageView  
	//        ImageView spaceshipImage = (ImageView) v.findViewById(R.id.img);  
	//        TextView tipTextView = (TextView) v.findViewById(R.id.tipTextView);// 提示文字  
	//        // 加载动画  
	//        Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(  
	//                context, R.anim.loading_animation);  
	//        // 使用ImageView显示动画  
	//        spaceshipImage.startAnimation(hyperspaceJumpAnimation);  
	//        tipTextView.setText(msg);// 设置加载信息  
	//  
	//        Dialog loadingDialog = new Dialog(context, R.style.loading_dialog);// 创建自定义样式dialog  
	//  
	////        loadingDialog.setCancelable(false);// 不可以用“返回键”取消  
	//        loadingDialog.setContentView(layout, new LinearLayout.LayoutParams(  
	//                LinearLayout.LayoutParams.FILL_PARENT,  
	//                LinearLayout.LayoutParams.FILL_PARENT));// 设置布局  
	//        loadingDialog.show();
	//        return loadingDialog;  
	//  
	//    }  
}
